<?php
/**

 */
defined('IN_IA') or exit('Access Denied');

function module_entries($name, $types = array(), $args = null) {
	$ts = array('rule', 'cover', 'menu', 'home', 'profile', 'shortcut');
	if(empty($types)) {
		$types = $ts;
	} else {
		$types = array_intersect($types, $ts);
	}
	$fields = implode("','", $types);
	$sql = 'SELECT * FROM ' . tablename('modules_bindings')." WHERE `module`=:module AND `entry` IN ('{$fields}') ORDER BY eid ASC";
	$pars = array();
	$pars[':module'] = $name;
	$bindings = pdo_fetchall($sql, $pars);
	$entries = array();
	foreach($bindings as $bind) {
		if(!empty($bind['call'])) {
			$site = WeUtility::createModuleSite($bind['module']);
			$ret = @$site->$bind['call']($args);
			if(is_array($ret)) {
				foreach($ret as $et) {
					$entries[$bind['entry']][] = array('title' => $et['title'], 'url' => $et['url'], 'from' => 'call');
				}
			}
		} else {
			if($bind['entry'] == 'cover') {
				$url = url("rule/cover", array('eid' => $bind['eid']));
			}
			if($bind['entry'] == 'menu') {
				$url = url("site/entry", array('eid' => $bind['eid']));
			}
			if($bind['entry'] == 'rule') {
				$url = url("site/entry", array('eid' => $bind['eid'], 'id' => '{id}'));
			}
			if($bind['entry'] == 'home') {
				$url = url("mobile/entry", array('eid' => $bind['eid']));
			}
			if($bind['entry'] == 'profile') {
				$url = url("mobile/entry", array('eid' => $bind['eid']));
			}
			if($bind['entry'] == 'shortcut') {
				$url = url("mobile/entry", array('eid' => $bind['eid']));
			}
			$entries[$bind['entry']][] = array('title' => $bind['title'], 'url' => $url, 'from' => 'define');
		}
	}
	return $entries;
}

function module_build_form($name, $rid) {
	$rid = intval($rid);
	$m = WeUtility::createModule($name);
	return $m->fieldsFormDisplay($rid);
}

function module_fetch($name) {
	$modules = uni_modules();
	return $modules[$name];
}